package com.MedDroid.task;

import android.os.AsyncTask;
import com.MedDroid.activity.AddAppointmentActivity;
import com.MedDroid.control.AuthenticationControl;
import com.MedDroid.control.DatabaseControl;
import com.MedDroid.control.ScheduleControl;
import com.MedDroid.entities.schedule.ScheduleEvent;

import java.lang.ref.WeakReference;
import java.sql.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Romen
 * Date: 02/12/13
 * Time: 11:00 PM
 * Description:
 */
public class AddNewAppointmentTask extends AsyncTask<Void, Void, Void>
{
	AuthenticationControl AuthenticationC;
	DatabaseControl DatabaseC;
	WeakReference<AddAppointmentActivity> AppointmentPage;

	public AddNewAppointmentTask(AuthenticationControl Auth, DatabaseControl Data, AddAppointmentActivity activity)
	{
		AuthenticationC = Auth;
		DatabaseC = Data;
		AppointmentPage = new WeakReference<AddAppointmentActivity>(activity);
	}

	@Override
	protected Void doInBackground(Void... voids)
	{
		AddAppointmentActivity Page = AppointmentPage.get();
		ScheduleEvent NewEvent = new ScheduleEvent();
		Date date = new Date(Page.datePicker.getYear(),Page.datePicker.getMonth(),Page.datePicker.getDayOfMonth());
		NewEvent.setDate(date.getTime());
		NewEvent.setDuration(Long.parseLong(Page.EditDuration.getText().toString()));
		NewEvent.setTitle(Page.EditTitle.getText().toString());
		NewEvent.setDetails(Page.EditReason.getText().toString());
		DatabaseC.addEvent(NewEvent, AuthenticationC.getLoginUser().getUserID());
		return null;
	}

	@Override
	protected void onPostExecute(Void V)
	{
		AddAppointmentActivity Page = AppointmentPage.get();
		Page.finish();
	}
}
